/*
 * BtnEdit.java
 *
 * Created on June 23, 2007, 4:02 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package latihanibatis.ui.event;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import latihanibatis.ui.MhsTabelModel;
import latihanibatis.ui.PanelStatus;

/**
 *
 * @author javamaniac
 */
public class BtnEdit extends AbstractAction {
    
    private final javax.swing.JTextField txtAlamat;
    private final javax.swing.JTextField txtKeterangan;
    private final javax.swing.JTextField txtKota;
    private final javax.swing.JFormattedTextField txtNIM;
    private final javax.swing.JTextField txtNama;
    private final MhsTabelModel tableModel;
    private final PanelStatus status;
    private final JTable tableMhs;
    private final JButton btnCari;
    private final JButton btnUpdate;
    private final JButton btnSimpan;
    private final JButton btnEdit;
    private final JButton btnDelete;
    
    /** Creates a new instance of BtnEdit */
    public BtnEdit(JFormattedTextField txt1, JTextField txt2, JTextField txt3,
        JTextField txt4, JTextField txt5, MhsTabelModel model, JTable tbl,
        PanelStatus status, JButton btnCari, JButton btnUpdate, JButton btnSimpan,
        JButton btnEdit, JButton btnDelete) {
        
        super();
        this.txtNIM        = txt1;
        this.txtNama       = txt2;
        this.txtKota       = txt3;
        this.txtAlamat     = txt4;
        this.txtKeterangan = txt5;
        this.tableModel    = model;
        this.status        = status;
        this.tableMhs      = tbl;
        this.btnCari       = btnCari;
        this.btnUpdate     = btnUpdate;
        this.btnSimpan     = btnSimpan;
        this.btnEdit       = btnEdit;
        this.btnDelete     = btnDelete;
    }
    
    public void actionPerformed(final ActionEvent e) {
        final int baris  = tableMhs.getSelectedRow(); // dimulai dari 0
        final int kolom  = tableMhs.getColumnCount(); // ambil smua jml kolom.
        final int jmlBrs = tableMhs.getRowCount();    // dimulai dari 1
        
        String isiTable = "";
        
        if (baris < 0 || baris >= jmlBrs) {
            status.setStatus("Tidak Ada Data Yang Ingin Di Edit !!", "ERROR");
            JOptionPane.showMessageDialog(null, "Tidak Ada Data Yang Ingin Di Edit !!",
                "Error !!!", JOptionPane.ERROR_MESSAGE);
            
            tableMhs.selectAll();
        } else {
            for (int i=baris; i == baris; i++) {
                for (int j=0; j < kolom; j++) {
                    
                    isiTable = tableModel.getValueAt(i,j).toString();
                    
                    // Ambil NIM Mhs yg akan dihapus.
                    if (j == 0)
                        txtNIM.setValue(Integer.parseInt(isiTable));
                    
                    if (j == 1)
                        txtNama.setText(isiTable);
                    
                    if (j == 2)
                        txtKota.setText(isiTable);
                    if (j == 3)
                        txtAlamat.setText(isiTable);
                    
                    if (j == 4)
                        txtKeterangan.setText(isiTable);
                } // end for j
                tableModel.setBarisForUpdate(i);
            } // end for i
            
            txtNIM.setEnabled(false);
            btnCari.setEnabled(false);
            btnSimpan.setEnabled(false);
            btnEdit.setEnabled(false);
            btnDelete.setEnabled(false);
            
            btnUpdate.setEnabled(true);
            
            // Give focus to field nama
            txtNama.requestFocusInWindow();
            txtNama.selectAll();
            
            status.setStatus("Tekan Tombol Update Untuk Mengupdate Data !!", "BANTUAN");
        } // end else
    }
}